package com.my.ycv51.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.my.ycv51.R;
import com.my.ycv51.constant.FaceConstant;
import com.my.ycv51.event.SimpleEvent;
import com.my.ycv51.single.SingleFace;
import com.my.ycv51.ycinit.config.Constants;
import com.my.ycv51.ycinit.opengl.GLFrameSurface;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CheckFaceDialogTwo extends Dialog implements View.OnClickListener {
    private String TAG = "【CheckFaceDialog】";
    private Activity context;
    private ImageView ivClose;
    private GLFrameSurface surfaceView;
    private TextView tvTip;
    private String userFace = null;

    private DialogCallBack dialogCallBack;


    public CheckFaceDialogTwo(@NonNull Context context, String userFace, DialogCallBack dialogCallBack) {
        super(context, R.style.MyEmptyDialog);
        this.context = (Activity) context;
        this.dialogCallBack = dialogCallBack;
        this.userFace = userFace;
    }

    @Override
    public void show() {
        super.show();
        DisplayMetrics d = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(d);
        android.view.WindowManager.LayoutParams p = this.getWindow().getAttributes();
        p.gravity = Gravity.CENTER;
        p.height = (int) (d.heightPixels * 0.8);
        p.width = (int) (d.widthPixels * 0.5);
        this.getWindow().setAttributes(p);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_regist_face);
        this.setCanceledOnTouchOutside(false);

        EventBus.getDefault().register(this);


        initView();
        initListener();

        SingleFace.getInstance().initCWCamear(context, surfaceView, FaceConstant.CODE_CHECK_FACE, userFace); // 验证人脸
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private void initView() {
        ivClose = findViewById(R.id.dialog_iv_cancle);
        surfaceView = findViewById(R.id.face_surfaceview);
        tvTip = findViewById(R.id.dialog_tip);
    }

    private void initListener() {
        ivClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_iv_cancle:
                closeDialog();
                break;
        }
    }

    public void closeDialog() {
        EventBus.getDefault().unregister(this);
        // 关闭流
        SingleFace.getInstance().close();
        dismiss();
    }

    //处理普通事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(SimpleEvent event) {
        switch (event.getCode()) {
            case FaceConstant.CODE_MESSAGE: // 提示文字
                tvTip.setText(event.getMsg());
                break;
            case FaceConstant.CODE_SUCCESS: // 成功
                if (dialogCallBack != null) {
                    dialogCallBack.checkSuccess();
                }

                closeDialog();
                break;
            case FaceConstant.CODE_FAIL: // 失败
                tvTip.setText(event.getMsg());
                break;
        }
    }

    public interface DialogCallBack {
        void onCancle();

        /**
         * 提示
         *
         * @param msg
         */
        void showMessage(String msg);

        /**
         * 验证成功
         */
        void checkSuccess();
    }
}
